package com.yunxi.controller.eai;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yunxi.common.config.RedisCacheService;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.eai.pojo.esb.EsbVO;
import com.yunxi.eai.service.esb.ESBService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created by Administrator on 2019/5/20.
 */
@RestController
@RequestMapping("/esb/api")
@Api(tags = "esb:manage", description = "接收esb信息")
public class ESBController {


/*    @Reference(version = "${demo.service.version}",
            application = "${dubbo.application.id}",
            registry = "${dubbo.registry.address}",
            // url = "${dubbo.application.url}",
            check = false,
            timeout = 10000
    )*/
    @Autowired
    private ESBService esbService;

    @Autowired
    private RedisCacheService rcs;
    @PostMapping("/receive")
    @ApiOperation(value = "接收ESB信息")
    public ApiResult receive(HttpServletRequest request, HttpServletResponse response) throws Exception {

        EsbVO esbVO=getEsbVO(request);
        ApiResult object=esbService.distributeEsb(esbVO);
        return object;
    }

    @PostMapping("/getEsbData")
    @ApiOperation(value = "获取ESB传递的信息")
    public ApiResult receive(String serviceName,String requestId) throws Exception {
        String requestKey="requestId:"+requestId+"-"+serviceName;
        Object object1 = rcs.get(requestKey);
        return ResponseUtil.ok(object1);
    }

    private EsbVO getEsbVO(HttpServletRequest request) throws IOException {
        EsbVO esbVO=new EsbVO();
        //获取表头name的值
        String serviceName = request.getHeader("serviceName");
        String sourceSystem = request.getHeader("sourceSystem");
        String requestId = request.getHeader("requestId");
        esbVO.setServiceName(serviceName);
        esbVO.setRequestId(requestId);
        esbVO.setSourceSystem(sourceSystem);
        // 读取请求内容
        BufferedReader br;
        String reqBody="";

        br = new BufferedReader(new InputStreamReader(request.getInputStream(),"UTF-8"));
        String line = null;
        StringBuilder sb = new StringBuilder();
        while((line = br.readLine())!=null){
            sb.append(line);
        }
        br.close();
        reqBody = sb.toString();
        esbVO.setReqBody(reqBody);
        //保存三天
        rcs.set("requestId:" + esbVO.getRequestId()+"-"+esbVO.getServiceName(), reqBody, 2592000);
        return esbVO;
    }
}
